package com.demo.order.controller;

import com.demo.common.base.pojo.JsonRes;
import com.demo.store.bo.CreateStoreBO;
import com.demo.store.vo.StoreMainVO;
import com.demo.order.bo.CreateOrderBO;
import com.demo.order.bo.LocalDateTimeBO;
import com.demo.order.entity.OrderMain;
import com.demo.order.exception.DemoOrderException;
import com.demo.order.service.DispatchRemoteStoreMainService;
import com.demo.order.service.DispatchRemoteUserMainService;
import com.demo.order.service.OrderMainService;
import com.demo.order.vo.LocalDateTimeVO;
import com.demo.user.bo.CreateUserBO;
import com.demo.user.bo.UserMainPageBO;
import com.demo.user.vo.UserMainVO;
import com.mybatisflex.core.paginate.Page;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * 订单查询
 *
 * @author DayDayUp
 * create on 2023-05-18
 **/
@Slf4j
@RestController
@RequestMapping("orderMain")
public class OrderMainController {
    @Resource
    private OrderMainService orderMainService;
    @Resource
    private DispatchRemoteUserMainService dispatchRemoteUserMainService;
    @Resource
    private DispatchRemoteStoreMainService dispatchRemoteStoreMainService;

    /**
     * 异常测试接口
     *
     * @return JsonRes
     */
//    @PreAuthorize("hasAuthority('user-list-edit')")
    @GetMapping("testException")
    public JsonRes<String> testException(HttpServletRequest request) {
        throw new DemoOrderException("订单异常");
    }

    /**
     * 测试限流接口
     *
     * @return JsonRes
     */
//    @PreAuthorize("hasAuthority('authorityList')")
    @GetMapping("testRateLimit")
    public JsonRes<String> testRateLimit(HttpServletRequest request) {
        System.out.println(request.getHeader("abc"));
        return JsonRes.ok("测试限流");
    }

    /**
     * 分页查询用户
     * @param userMainPageBO 分页参数
     * @return JsonRes
     */
    @GetMapping("pageUserMain")
    public JsonRes<Page<UserMainVO>> pageUserMain(UserMainPageBO userMainPageBO) {
        log.info("OrderMainController-pageUserMain");
        Page<UserMainVO> userMainPage = dispatchRemoteUserMainService.pageUserMain(userMainPageBO);
        return JsonRes.ok(userMainPage);
    }

    /**
     * 查詢庫存
     * @param id 分页参数
     * @return JsonRes
     */
    @GetMapping("getOneStoreMain")
    public JsonRes<StoreMainVO> pageUserMain(Long id) {
        StoreMainVO storeMainVO = dispatchRemoteStoreMainService.getOneStoreMain(id);
        return JsonRes.ok(storeMainVO);
    }


    /**
     * 查詢庫存
     * @param localDateTimeBO 分页参数
     * @return JsonRes
     */
    @PostMapping("parseLocalDateTime")
    public JsonRes<LocalDateTimeVO> parseLocalDateTime(LocalDateTimeBO localDateTimeBO) {
        log.info("时间格式化：{}",localDateTimeBO.toString());
        LocalDateTimeVO localDateTimeVO = new LocalDateTimeVO();
        localDateTimeVO.setLocalDate(LocalDate.now());
        localDateTimeVO.setLocalDateTime(LocalDateTime.now());
        localDateTimeVO.setName("时间测试");
        return JsonRes.ok(localDateTimeVO);
    }

    /**
     * 下单，测试分布式事务
     */
    @GlobalTransactional
    @PostMapping("createOrder")
    public JsonRes<String> createOrder(@RequestBody CreateOrderBO createOrderBO) {
        // 创建订单
        OrderMain orderMain = new OrderMain();
        orderMain.setOrderNo(createOrderBO.getOrderNo());
        orderMainService.save(orderMain);

        // 创建用户
        CreateUserBO createUserBO = new CreateUserBO();
        createUserBO.setAge(createOrderBO.getAge());
        createUserBO.setUserId(createOrderBO.getUserId());
        createUserBO.setSex(createOrderBO.getSex());
        createUserBO.setUsername(createOrderBO.getUsername());
        dispatchRemoteUserMainService.createUser(createUserBO);

        // 创建库存
        CreateStoreBO createStoreBO = new CreateStoreBO();
        createStoreBO.setProductName(createOrderBO.getProductName());
        createStoreBO.setProductTotal(createOrderBO.getProductTotal());
        createStoreBO.setProductSurplus(createOrderBO.getProductSurplus());
        dispatchRemoteStoreMainService.createStore(createStoreBO);

        return JsonRes.ok("创建成功");
    }
}
